package movie.test.movie;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import movie.test.as.movie.service.MovieInfoService;

@RestController
@RequestMapping("/movieInfo")
public class MovieInfoController {

	private static Logger logger = LoggerFactory
			.getLogger(MovieInfoController.class);
	@Autowired
	private MovieInfoService movieInfoServiceImpl;
	

	@RequestMapping("/dd")
	public void movieTest() {
		System.out.println("ttttttttttt");
	}

	/**
	 * @author lwy
	 * @deprecated:正在热映的影片和即将上映的影片前十部
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */
	@ResponseBody
	@RequestMapping("/getMovieInfoList")
	public Object getMovieInfoList(@RequestBody JSONObject objs, HttpServletRequest request) {
		try {
			logger.debug("开始查询正在热映的影片信息---");
			List<Map<String, Object>> pageResult = movieInfoServiceImpl.getMovieInfoList(objs);
			logger.info("查询正在热映的影片信息结束---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", pageResult);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@ResponseBody
	@RequestMapping("/getMovieInfoWellList")
	public Object getMovieInfoWellList(@RequestBody JSONObject objs, HttpServletRequest request) {
		try {
			logger.debug("开始查询正在热映的影片信息---");
			List<Map<String, Object>> pageResult = movieInfoServiceImpl.getMovieInfoWellList(objs);
			logger.info("查询正在热映的影片信息结束---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", pageResult);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * @author lwy
	 * @deprecated:正在热映的影片和即将上映的影片
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */
	@ResponseBody
	@RequestMapping("/getAllMovieInfoList")
	public Object getAllMovieInfoList(@RequestBody JSONObject objs, HttpServletRequest request) {
		try {
			logger.debug("开始查询正在热映的影片信息---");
		List<Map<String, Object>> pageResult = movieInfoServiceImpl.getAllMovieInfoList(objs);
			logger.info("查询正在热映的影片信息结束---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", pageResult);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 影片总数
	 */
	
	@ResponseBody
	@RequestMapping("/getCountMovieInfo")
	public Object getCountMovieInfo(@RequestBody JSONObject objs, HttpServletRequest request) {
		try {
			logger.debug("查询影片总数---");
			int count  = movieInfoServiceImpl.getCountMovieInfo(objs);
			logger.info("查询影片的总数---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", count);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

	/**
	 * @author lwy
	 * @deprecated:影片信息添加
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */

	/**
	 * @author lwy
	 * @deprecated:影片信息修改
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */
	@ResponseBody
	@RequestMapping(value = { "/addMovieInfo" }, method = {
			RequestMethod.GET, RequestMethod.POST })
	public Object addMovieInfo(@RequestBody JSONObject objs,HttpServletRequest request){
		 try{
			 logger.debug("添加影片的信息开始---");
			int count = movieInfoServiceImpl.addMovieInfo(objs);
			 logger.info("添加影片信息结束");
			 LinkedHashMap<String,Object> resultMap = new LinkedHashMap<String,Object>();
			 if(count>0){
				 resultMap.put("successcode", count);
			 }else{
				 resultMap.put("errorcode", count);
			 }
			 return resultMap;
		 } catch(Exception e){
			 e.printStackTrace();
		 }
		return "";
		 
	 }
	 
	/**
	 * @author lwy
	 * @deprecated:影片信息删除
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */
	@ResponseBody
	@RequestMapping("/deleteMovieInfoById")
	public Object deleteMovieInfoById(@RequestBody JSONObject objs,
			HttpServletRequest request) {
		try {
			logger.debug("开始删除影片信息---");
			int count = movieInfoServiceImpl.deleteMovieInfoById(objs);
			logger.info("删除的影片信息结束---");
			 LinkedHashMap<String,Object> resultMap = new LinkedHashMap<String,Object>();
			 if(count>0){
				 resultMap.put("successcode", count);
			 }else{
				 resultMap.put("errorcode", count);
			 }
			 return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * @author lwy
	 * @deprecated:影片信息查看
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */
	@ResponseBody
	@RequestMapping("/getMovieInfoById")
	public Object getMovieInfoById(@RequestBody JSONObject objs,HttpServletRequest request){
		try{
			logger.debug("查看影片的信息开始----");
		    Map<String,Object> mapInfo = movieInfoServiceImpl.getMovieInfoById(objs);
			logger.info("查看影片信息结束...");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", mapInfo);
			return linkedHashMap;
		} catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	
	@ResponseBody
	@RequestMapping("/getSysAllMovieInfoList")
	public Object getSysAllMovieInfoList(@RequestBody JSONObject objs, HttpServletRequest request) {
		try {
			logger.debug("查询所有电影信息---");
			 PageInfo <Map<String, Object>> pageResult = movieInfoServiceImpl.getSysAllMovieInfoList(objs);
			logger.info("查询所有电影信息结束---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", pageResult);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping("/getSysfindAllHall")
	public Object getSysfindAllHall(HttpServletRequest request) {
		try {
			logger.debug("查询所有电影信息---");
			List<Map<String, Object>> pageResult = movieInfoServiceImpl.getSysfindAllHall();
			logger.info("查询所有电影信息结束---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", pageResult);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@ResponseBody
	@RequestMapping("/findAllCategory")
	public Object findAllCategory(HttpServletRequest request) {
		try {
			logger.debug("查询所有电影信息---");
			List<Map<String, Object>> pageResult = movieInfoServiceImpl.findAllCategory();
			logger.info("查询所有电影信息结束---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", pageResult);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping(value = { "/updateMovieInfo" }, method = {
			RequestMethod.GET, RequestMethod.POST })
	public Object updateMovieInfo(@RequestBody JSONObject objs,HttpServletRequest request){
		 try{
			 logger.debug("修改影片的信息开始---");
			int count = movieInfoServiceImpl.updateMovieInfo(objs);
			 logger.info("修改影片信息结束");
			 LinkedHashMap<String,Object> resultMap = new LinkedHashMap<String,Object>();
			 if(count>0){
				 resultMap.put("successcode", count);
			 }else{
				 resultMap.put("errorcode", count);
			 }
			 return resultMap;
		 } catch(Exception e){
			 e.printStackTrace();
		 }
		return "";
		 
	 }
	/**
	 * 上传影片信息
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@ResponseBody
	@RequestMapping(value={"/uploadMovieById"},method={RequestMethod.GET,RequestMethod.POST})
	public Object uploadMovieById(@RequestParam MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException{
	try{	  
		String id = request.getParameter("id");  
		  LinkedHashMap<String,Object> result = new LinkedHashMap<String,Object>();
		if(!file.isEmpty()){
			 String fileName = file.getOriginalFilename();
			 String filePathName = "upload/"+fileName;
		   File targetFile = new File("E:\\upload",fileName);
		   if(!targetFile.exists()){
			   targetFile.mkdirs();
		   }
				file.transferTo(targetFile);
				JSONObject objs = new JSONObject();
				objs.put("id", id);
				objs.put("filePathName", filePathName);
				int count = movieInfoServiceImpl.updateUploadMovie(objs);
				 result.put("successcode",count); 
		}
		
		return result;
	 } catch(Exception e){
		 e.printStackTrace();
	 }
	return "";
			    	
			    
	}
	
	/**
	 * 上传影片信息
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@ResponseBody
	@RequestMapping(value={"/uploadVideoById"},method={RequestMethod.GET,RequestMethod.POST})
	public Object uploadVideoById(@RequestParam MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException{
	try{	  
		String id = request.getParameter("id");  
		  LinkedHashMap<String,Object> result = new LinkedHashMap<String,Object>();
		if(!file.isEmpty()){
			 String fileName = file.getOriginalFilename();
			 String filePathName = "upload/"+fileName;
			 //System.out.println(fileName);//ww.jpg
			//String path = request.getSession().getServletContext().getRealPath("upload");
		   File targetFile = new File("E:\\upload",fileName);
		   if(!targetFile.exists()){
			   targetFile.mkdirs();
		   }
				file.transferTo(targetFile);
				JSONObject objs = new JSONObject();
				objs.put("id", id);
				objs.put("filePathName", filePathName);
				int count = movieInfoServiceImpl.uploadVideoById(objs);
				 result.put("successcode",count); 
		}
		
		return result;
	 } catch(Exception e){
		 e.printStackTrace();
	 }
	return "";
	}		    	
			    
	
	/**
	 * 上传演员的信息
	 * @param file
	 * @param request
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value={"/uploadMoviePersonById"},method={RequestMethod.GET,RequestMethod.POST})
	public Object uploadMoviePersonById(@RequestParam MultipartFile[] file,HttpServletRequest request) throws IllegalStateException, IOException{
	try{	  
		Integer id = Integer.parseInt(request.getParameter("id"));
		
		List personList = new ArrayList();
		String personName1= request.getParameter("personName1");
		System.out.println(personName1);
		String personName2= request.getParameter("personName2");
		System.out.println(personName2);
		String personName3= request.getParameter("personName3");
		System.out.println(personName3);
		String personName4= request.getParameter("personName4");
		String personName5= request.getParameter("personName5");
		personList.add(personName1);
		personList.add(personName2);
		personList.add(personName3);
		personList.add(personName4);
		personList.add(personName5);
		String filePathName = null;
		int flag=0;
		List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
		
		if(file.length>0){
			for(int i=0;i<file.length;i++){
				String fileName = file[i].getOriginalFilename();//得到文件的名称
				if((fileName!=null && !"".equals("fileName")&& personList.get(i)!=null && !"".equals(personList.get(i)))){
			     filePathName = "upload/"+fileName+System.currentTimeMillis();//文件名称不能重复
			     File targetFile = new File("E:\\upload",fileName+System.currentTimeMillis());
				 if(!targetFile.exists()){
					   targetFile.mkdirs();
				   }
				   file[i].transferTo(targetFile);
					flag++;
				   Map<String,Object> personMap = new LinkedHashMap<String,Object>();
				   personMap.put("filePathName", filePathName);
				   String personName = (String) personList.get(i);
				   personMap.put("personName", personName);
				   personMap.put("moviePersonId", id);
				   dataList.add(personMap);
			 }
			}
		}
		int count = movieInfoServiceImpl.updateUploadPersonMovieInfo(dataList);
		Map<String,Object> result = new LinkedHashMap<String,Object>();
		result.put("successcode", count);
		return result;
		
	 } catch(Exception e){
		 e.printStackTrace();
	 }
	return "";
	}

}
